<template>
  <div class="dialogRole">
    <el-dialog :visible.sync="showRole" title="编辑角色" @close="closeEvent">
      <el-checkbox-group v-model="checkArr">
        <el-checkbox
          v-for="item in checkboxList"
          :key="item.id"
          :label="item.id"
          >{{ item.name }}</el-checkbox
        >
      </el-checkbox-group>
      <template #footer>
        <div class="footer">
          <el-button @click="submit" type="primary">提交</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  name: 'DialogRole',
  data () {
    return {
      showRole: false,
      id: '',
      checkboxList: [],
      checkArr: []
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 获取员工基本信息(角色)
    async getUserInfo (id) {
      this.id = id
      const res = await sysUser(id)
      this.checkArr = res.data.roleIds
    //   console.log('当前员工个人信息', res)
    },

    // 获取角色列表
    async getData () {
      const res = await sysRole({ pagesize: 100 })
      this.checkboxList = res.data.rows
    //   console.log('角色列表', res)
    },
    async submit () {
      await sysUserAssignRoles(this.id, this.checkArr)
      this.$message.success('分配角色成功')
      this.closeEvent()
    },
    closeEvent () {
      this.showRole = false
      this.checkArr = []
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  text-align: center;
}
</style>
